﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.BusinessCard;
using PetaPoco;

namespace BrnMall.Manager.Manager.BusinessCard
{
    /// <summary>
    /// 用户名片
    /// </summary>
    public static class UserCardManager
    {
        public static UserCard GetUserCard(int uid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<UserCard>("select * from bc_usercard where uid=@0", uid);
            }
        }
        public static Page<UserCard> GetUserActionCard(int uid,int actionType, long pageNumber, long pageSize)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Page<UserCard>(pageNumber, pageSize, "select * from bc_usercard where cid in(select distinct cid from bc_cardaction where actionuid=@0 and actiontype=@1)", uid,actionType);
            }
        }

        public static Page<UserCard> GetStoreUserCard(int storeId,long pageNumber, long pageSize)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Page<UserCard>(pageNumber,pageSize, "select * from bc_usercard where storeid=@0", storeId);
            }
        }

        public static void InsertUserCard(UserCard card)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(card);
            }
        }

        public static void UpdateUserCard(UserCard card)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(card);
            }
        }

        public static void UpdateCardCode(int cid,string cardCode)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update bc_usercard set cardcode=@0 where cid=@1", cardCode, cid);
            }
        }

        public static void InsertCardAction(CardAction action)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(action);
                switch (action.ActionType)
                {
                    case 1:
                        db.Execute("update bc_usercard set praised=praised+1 where cid=@0", action.CId);
                        break;
                    case 2:
                        db.Execute("update bc_usercard set favorite=favorite+1 where cid=@0", action.CId);
                        break;
                    case 3:
                        db.Execute("update bc_usercard set views=views+1 where cid=@0", action.CId);
                        break;
                    default:
                        break;
                }
            }
        }

        public static void DeleteCardAction(CardAction action)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete(action);
                switch (action.ActionType)
                {
                    case 1:
                        db.Execute("update bc_usercard set praised=praised-1 where cid=@0", action.CId);
                        break;
                    case 2:
                        db.Execute("update bc_usercard set favorite=favorite-1 where cid=@0", action.CId);
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
       /// 获取用户的名片操作
       /// </summary>
       /// <param name="cid"></param>
       /// <param name="uid"></param>
       /// <param name="actionType"></param>
       /// <returns></returns>
        public static CardAction GetCardAction(int cid, int uid, int actionType)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<CardAction>(
                    "select * from bc_cardaction where cid=@0 and actionuid=@1 and actiontype=@2", cid, uid, actionType);
            }
        }

        public static Page<CardAction> GetCardActionPage(int cid, int actiontype = -1, long pageNumber=1,
            long itemsPerPage=15)
        {
            using (var db = new QDMallDatabase())
            {
                var sql=new Sql();
                sql.Select("*");
                sql.From("bc_cardaction");
                if (actiontype != -1)
                    sql.Where("cid=@0 and actiontype=@1", cid, actiontype);
                else
                    sql.Where("cid=@0", cid);
                return db.Page<CardAction>(pageNumber, itemsPerPage, sql);
            }
        }
        public static Page<CardAction> GetUserCardActionPage(int uid, int actiontype = -1, long pageNumber = 1,
            long itemsPerPage = 15)
        {
            using (var db = new QDMallDatabase())
            {
                var sql = new Sql();
                sql.Select("*");
                sql.From("bc_cardaction");
                if (actiontype != -1)
                    sql.Where("actionuid=@0 and actiontype=@1", uid, actiontype);
                else
                    sql.Where("actionuid=@0", uid);
                return db.Page<CardAction>(pageNumber, itemsPerPage, sql);
            }
        }
        /// <summary>
        /// 名片认证
        /// </summary>
        /// <param name="verify"></param>
        public static void AddCardVerify(CardVerify verify)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(verify);
            }
        }

        public static CardVerify GetCardVerify(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<CardVerify>(id);
            }
        }
        public static CardVerify GetCardVerifyByUId(int uid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<CardVerify>("select top 1 * from bc_cardverify where uid=@0",uid);
            }
        }
        public static Page<CardVerify> GetStoreCardVerify(int storeId, long pageNumber, long itemsPerPage)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Page<CardVerify>(pageNumber, itemsPerPage,
                    "select * from bc_cardverify where storeid=@0 order by sys_createtime desc", storeId);
            }
        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id"></param>
        /// <param name="verifyUid"></param>
        /// <param name="state"></param>
        public static void VerifyCard(int id, int verifyUid, int state)
        {
            using (var db = new QDMallDatabase())
            {
                var verify = db.SingleOrDefault<CardVerify>(id);
                if (verify == null) return;
                verify.VerifyUid = verifyUid;
                verify.VerifyTime=DateTime.Now;
                verify.VerifyState = state;
                db.Update(verify);
                db.Execute("update bc_usercard set isverify=@0 where uid=@1", state==2?1:0, verify.UId);
            }
           
        }
    }
}
